package com.he.controller;

import com.he.annotation.AccessLimit;
import com.he.domain.Result;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.dto.PageResult;
import com.he.domain.dto.TagDTO;
import com.he.domain.vo.TagBackVO;
import com.he.service.TagService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/18 11:47
 * @Desc 标签控制器
 */
@Tag(name = "标签模块")
@RestController
public class TagController {

    @Resource
    private TagService tagService;

    @Operation(description = "查看标签列表")
    @GetMapping("/tag/list")
    public Result<?> listTagVO(){
        if (tagService != null) {
            return Result.success(tagService.listTagVO());
        } else {
            return Result.success(Collections.emptyList());
        }
    }

    @Operation(description = "查看标签选项")
    @AccessLimit(seconds = 1, maxCount = 3)
    @GetMapping("/admin/tag/option")
    public Result<?> listTagBackOption(){
        return Result.success(tagService.listTagOption());
    }

    @Operation(description = "查看标签列表文章")
    @GetMapping("/tag/article/list")
    public Result<?> listTagArticleVO(ConditionDTO conditionDTO){
        return Result.success(tagService.listArticleTag(conditionDTO));
    }

    /**
     * 查看后台标签列表
     *
     * @param condition 查询条件
     * @return {@link TagBackVO} 后台标签
     */
    @Operation(description = "查看后台标签列表")
    @PreAuthorize("@ps.hasPermission('blog:tag:list')")
    @GetMapping("/admin/tag/list")
    public Result<PageResult<TagBackVO>> listTagBackVO(ConditionDTO condition) {
        return Result.success(tagService.listTagBackVO(condition));
    }

    /**
     * 添加标签
     *
     * @param tag 标签信息
     * @return {@link Result<>}
     */
    @Operation(description = "添加标签")
    @PreAuthorize("@ps.hasPermission('blog:tag:add')")
    @PostMapping("/admin/tag/add")
    public Result<?> addTag(@Validated @RequestBody TagDTO tag) {
        tagService.addTag(tag);
        return Result.success();
    }

    /**
     * 删除标签
     *
     * @param tagIdList 标签id集合
     * @return {@link Result<>}
     */
    @Operation(description = "删除标签")
    @PreAuthorize("@ps.hasPermission('blog:tag:delete')")
    @DeleteMapping("/admin/tag/delete")
    public Result<?> deleteTag(@RequestBody List<Integer> tagIdList) {
        tagService.deleteTag(tagIdList);
        return Result.success();
    }

    /**
     * 修改标签
     *
     * @param tag 标签信息
     * @return {@link Result<>}
     */
    @Operation(description = "修改标签")
    @PreAuthorize("@ps.hasPermission('blog:tag:update')")
    @PutMapping("/admin/tag/update")
    public Result<?> updateTag(@Validated @RequestBody TagDTO tag) {
        tagService.updateTag(tag);
        return Result.success();
    }
}
